#ifndef UTF_H
#define UTF_H

#include <string>
#include <vector>
#include <cstdint>

class UTF8Validator {
public:
    // 验证单个UTF-8字符的完整性
    static bool isValidUTF8Char(const uint8_t* data, size_t length, size_t pos) {
        if (pos >= length) return false;
        
        uint8_t first = data[pos];
        
        // 单字节字符 (0xxxxxxx)
        if ((first & 0x80) == 0x00) {
            return true;
        }
        
        // 多字节字符
        int expected_len = 0;
        if ((first & 0xE0) == 0xC0) {        // 2字节字符
            expected_len = 2;
        } else if ((first & 0xF0) == 0xE0) { // 3字节字符
            expected_len = 3;
        } else if ((first & 0xF8) == 0xF0) { // 4字节字符
            expected_len = 4;
        } else {
            return false; // 非法起始字节
        }
        
        // 检查是否有足够字节
        if (pos + expected_len > length) {
            return false;
        }
        
        // 验证后续字节格式 (10xxxxxx)
        for (int i = 1; i < expected_len; i++) {
            if ((data[pos + i] & 0xC0) != 0x80) {
                return false;
            }
        }
        
        // 检查编码范围有效性
        return isValidCodePoint(data + pos, expected_len);
    }
    
    // 过滤无效UTF-8序列，用替换字符代替
    static std::string filterUTF8(const std::string& input, 
                                 char replacement = '?') {
        std::string result;
        result.reserve(input.length());
        
        const uint8_t* data = reinterpret_cast<const uint8_t*>(input.data());
        size_t length = input.length();
        size_t i = 0;
        
        while (i < length) {
            if (isValidUTF8Char(data, length, i)) {
                uint8_t first = data[i];
                int char_len = getUTF8CharLength(first);
                
                // 添加有效字符
                result.append(input.substr(i, char_len));
                i += char_len;
            } else {
                // 替换无效字节
                result += replacement;
                i++;
            }
        }
        
        return result;
    }
    
    // 严格模式：移除所有无效序列
    static std::string strictFilterUTF8(const std::string& input) {
        std::string result;
        result.reserve(input.length());
        
        const uint8_t* data = reinterpret_cast<const uint8_t*>(input.data());
        size_t length = input.length();
        size_t i = 0;
        
        while (i < length) {
            if (isValidUTF8Char(data, length, i)) {
                uint8_t first = data[i];
                int char_len = getUTF8CharLength(first);
                result.append(input.substr(i, char_len));
                i += char_len;
            } else {
                // 跳过无效字节
                i++;
            }
        }
        
        return result;
    }

private:
    static int getUTF8CharLength(uint8_t first_byte) {
        if ((first_byte & 0x80) == 0x00) return 1;
        if ((first_byte & 0xE0) == 0xC0) return 2;
        if ((first_byte & 0xF0) == 0xE0) return 3;
        if ((first_byte & 0xF8) == 0xF0) return 4;
        return 1; // 默认按单字节处理
    }
    
    static bool isValidCodePoint(const uint8_t* bytes, int length) {
        if (length == 1) {
            return true; // 单字节总是有效
        }
        
        uint32_t code_point = 0;
        
        switch (length) {
            case 2:
                code_point = ((bytes[0] & 0x1F) << 6) | (bytes[1] & 0x3F);
                return code_point >= 0x80; // 2字节字符范围: U+0080 - U+07FF
                
            case 3:
                code_point = ((bytes[0] & 0x0F) << 12) | ((bytes[1] & 0x3F) << 6) | (bytes[2] & 0x3F);
                return code_point >= 0x800 && 
                       !(code_point >= 0xD800 && code_point <= 0xDFFF); // 排除代理对
                
            case 4:
                code_point = ((bytes[0] & 0x07) << 18) | ((bytes[1] & 0x3F) << 12) | 
                            ((bytes[2] & 0x3F) << 6) | (bytes[3] & 0x3F);
                return code_point >= 0x10000 && code_point <= 0x10FFFF;
                
            default:
                return false;
        }
    }
};

#endif